/*
 * MIT License
 *
 * Copyright (c) 2019 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.commons.file;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import cn.seaboot.commons.digest.Base64;

/**
 * Base64字符串-图片 互相转换
 *
 * @author Mr.css
 * @version 2019/10/18 9:17
 */
public class Base64ImageUtils {
    public static final String PREFIX = "data:image/png;base64,";

    private Base64ImageUtils() {
    }

    /**
     * 用于抓取 URL 中的图片信息，本函数通常应该在 js 脚本进行处理。
     * <p>
     * e.g. background-image:url(data:image/png;base64,${base64Image});
     *
     * @param uri 图片地址
     * @return 获取图片URL中的base64图片
     */
    public static String getBase64FromUrl(String uri) {
        if (uri.startsWith(PREFIX)) {
            return uri.substring(PREFIX.length());
        }
        return uri;
    }

    /**
     * Base64 数据转图片
     *
     * @param base64 base64 字符串
     * @param os     输出IO
     * @throws IOException -
     */
    public static void base64ToImage(String base64, OutputStream os) throws IOException {
        os.write(Base64.decodeString(base64));
        os.flush();
    }

    /**
     * 图片转化成 base64 字符串
     *
     * @param is in
     * @return base64
     * @throws IOException -
     */
    public static String imageToBase64(InputStream is) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        IOUtils.copy(is, output);
        byte[] data = output.toByteArray();
        return Base64.encodeString(data);
    }

    /**
     * 图片转化成 base64 字符串
     *
     * @param image in
     * @return base64
     * @throws IOException -
     */
    public static String imageToBase64(BufferedImage image, String formatName) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, formatName, os);
        return Base64.encodeString(os.toByteArray());
    }

    /**
     * 图片转化成 base64 字符串
     *
     * @param data 字节码数据
     * @return base64
     * @throws IOException -
     */
    public static String bytesToBase64Url(byte[] data) throws IOException {
        return PREFIX + Base64.encodeString(data);
    }

    /**
     * 图片转化成 base64 字符串
     *
     * @param is in
     * @return base64
     * @throws IOException -
     */
    public static String imageToBase64Url(InputStream is) throws IOException {
        return PREFIX + Base64ImageUtils.imageToBase64(is);
    }


    /**
     * 图片转化成 base64 字符串
     *
     * @param image in
     * @return base64
     * @throws IOException -
     */
    public static String imageToBase64Url(BufferedImage image, String formatName) throws IOException {
        return PREFIX + Base64ImageUtils.imageToBase64(image, formatName);
    }
}
